package com.itic.system.collage.imageVideo.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.type.ImageVideoType;
import com.itic.system.vo.ImageVieoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.system.collage.imageVideo.persistence.dao.CollageImageVideoDao;
import com.itic.system.collage.imageVideo.persistence.model.CollageImageVideo;
import com.itic.system.collage.imageVideo.persistence.model.CollageImageVideoExample;
import com.itic.system.collage.imageVideo.persistence.model.CollageImageVideoExample.Criteria;

import java.util.List;


@Service
@Transactional
public class CollageImageVideoService
     extends CrudService<CollageImageVideoDao,CollageImageVideo,CollageImageVideoExample> {
    @Autowired
    private CollageImageVideoDao collageImageVideoDao;

    /**
     * 添加或者编辑拼团图片视频
     * @param collageId
     * @param imageVieoVos
     * @return
     */
    public  void  addOrEditCollageImageVideo(String collageId, List<ImageVieoVo> imageVieoVos){
        //查看该拼团是否存在图片和视频
        List<CollageImageVideo> collageImageVideos = this.findCollageImageVideoByCollageId(collageId);
        if(!collageImageVideos.isEmpty())
            //删除所有的图片和视频
            this.delBatchCollageImageVideo(collageImageVideos);
        //添加图片和视频
        for(ImageVieoVo imageVieoVo:imageVieoVos){
            ImageVideoType imageVieoType = ImageVideoType.getImageVieoType(imageVieoVo.getType());
            if(imageVieoType !=null){
                this.addCollageImageVideo(collageId,imageVieoType,imageVieoVo.getUrl());
            }else
                continue;
        }

    }

    /**
     * 添加拼团图片视频
     * @param collageId
     * @param imageVieoType
     * @param url
     * @return
     */
    public String addCollageImageVideo(String collageId, ImageVideoType imageVieoType, String url){
        CollageImageVideo collageImageVideo = new CollageImageVideo();
        collageImageVideo.setCollageId(collageId);
        collageImageVideo.setType(imageVieoType.getType());
        collageImageVideo.setUrl(url);
        String result = this.add(collageImageVideo);
        return result;
    }

    /**
     * 根据拼团Id查询图片和视频
     * @param collageId 拼团Id
     * @return
     */
    public List<CollageImageVideo> findCollageImageVideoByCollageId(String collageId){
        CollageImageVideoExample example = new CollageImageVideoExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andCollageIdEqualTo(collageId);
        List<CollageImageVideo> collageImageVideos = this.list(example);
        return collageImageVideos;
    }

    /**
     * 批量删除图片和视频
     * @param collageImageVideos
     */
    public void delBatchCollageImageVideo(List<CollageImageVideo> collageImageVideos){
        for(CollageImageVideo collageImageVideo : collageImageVideos)
            this.delete(collageImageVideo);
    }

    /**
     * 通过拼团Id 以及 图片视频类型查询拼团对应的类型集合
     * @param collageId
     * @param imageVideoType
     * @return
     */
    public List<CollageImageVideo> findCollageImageVideoByCollageIdAndType(String collageId, ImageVideoType imageVideoType) {
        CollageImageVideoExample example = new CollageImageVideoExample();
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andCollageIdEqualTo(collageId);
        criteria.andTypeEqualTo(imageVideoType.getType());
        List<CollageImageVideo> collageImageVideos = this.list(example);
        return collageImageVideos;
    }


}